global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A33_addcontrols.log, replace name(tabl)

capture noi { 
	
	* Additional control regressions
	* v8

	* ------------------- *
	* Prepare baseline reg
	* ------------------- *

	estimates clear
	do ${code_dir}/config/tabletools.do

	use ${final_dir}/regression_dataset${weight_window}${tf}.dta, clear

	clonevar LSW = lswMPm_1995_a
	clonevar HSW = hswMPm_1995_a
	clonevar VAEMP = vaempMPm_1995_a
	clonevar GDPGAP = lngdpgap_1995_a
	clonevar GDPPC = gdppcMPm_1995_a

	clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
	clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a
	clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
	clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a	
	clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a

	clonevar stockown = k${depvar}_${ttt} 
	clonevar stockownzero = k${depvar}_${ttt}0  
	clonevar stockother = kNOT_${depvar}_${ttt} 
	clonevar stockotherzero = kNOT_${depvar}_${ttt}0

	clonevar spilloversown = spill${depvar}${ttt}_1995_a
	clonevar spilloversother = spillN${depvar}${ttt}_1995_a
	clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
	clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0

	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995
	sort lse_id year
	egen yearctry = group(year country_shr_1995) if year <= 2009
	egen yearctrylintr = group(year country_shr_lintr_1995) if year <= 2009
	egen yearindustry = group(year industry) if year <= 2009
	egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009


	* ------------------- *
	* Offshoring
	* ------------------- *

	mmerge BvD year using ${final_dir}/bvd_year_offshoring_sharesgdpweighted_from1970_tfacit1.dta, unmatched(master)
	sort lse_id year
	clonevar offshoring = offshoring_ALL_1995_wtd
	clonevar offshoring_foreign = offshoring_shr_foreign_1995_wtd

	* run offshoring

	*1) Offshoring | F + IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP  offshoring VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store o_fd1

	*4) Offshoring | F + IY + CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP  offshoring VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store o_fd2

	* Offshoring | F + IY + CY | foreign
	ppmlhdfe F2.${depvar}_${ttt}  LSW_foreign HSW_foreign GDPgap_foreign offshoring_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store o_f

	* ------------------- *
	* IW wages
	* ------------------- *

	* prepare and run inventor weighted wages
	global iw "iw"
	mmerge BvD year using ${final_dir}/part_inventor_weighted_wages.dta, unmatched(master)
	drop _m
	sort lse_id year
	clonevar LSW${iw} = lswMPm_1995_a${iw}
	clonevar HSW${iw} = hswMPm_1995_a${iw}
	clonevar LSW_foreign${iw} = lswMPm_shr4_foreign_1995_a${iw}
	clonevar HSW_foreign${iw} = hswMPm_shr4_foreign_1995_a${iw}

	* 3) inventor weighted wages (H/L) | F + IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP LSW${iw} HSW${iw} stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store iw_fd1

	* 6) inventor weighted wages (H/L) | F + IY + CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP LSW${iw} HSW${iw} stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store iw_fd2

	* inventor weighted wages (H/L) | F + IY + CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign LSW_foreign${iw} HSW_foreign${iw} stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store iw_f

	* ------------------- *
	* Lintr 
	* ------------------- *

	* prepare and run interest rate
	drop LSW HSW VAEMP GDPGAP GDPPC *_foreign
	clonevar LSW = lswMPy_1995_a
	clonevar HSW = hswMPy_1995_a
	clonevar VAEMP = vaempMPy_1995_a
	clonevar GDPGAP = lngdpgapy_1995_a
	clonevar Lintr = lintrMPy_1995_a
	clonevar GDPPC = gdppcMPy_1995_a

	clonevar GDPPC_foreign = gdppcMPy_shr4_foreign_1995_a
	clonevar VAEMP_foreign = vaempMPy_shr4_foreign_1995_a
	clonevar GDPgap_foreign = lngdpgapy_shr_foreign_1995_a
	clonevar Lintr_foreign = lintrMPy_shr_foreign_1995_a
	clonevar LSW_foreign = lswMPy_shr4_foreign_1995_a
	clonevar HSW_foreign = hswMPy_shr4_foreign_1995_a

	* 2) Long-term interest rate | F + IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP Lintr VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_lintr_1995 < 1 & missing_weights_lintr_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,  vce(cluster lse_id)  absorb(lse_id yearindustry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store i_fd1

	* 5) Long-term interest rate | F + IY + CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP Lintr VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_lintr_1995 < 1 & missing_weights_lintr_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,  vce(cluster lse_id)  absorb(lse_id yearindustry yearctrylintr)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store i_fd2
	
	* Long-term interest rate | F + IY + CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign Lintr_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_lintr_1995 < 1 & missing_weights_lintr_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,  vce(cluster lse_id)  absorb(lse_id yearindustry yearctrylintr)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store i_f

	* ------------------- *
	* Output tables
	* ------------------- *
	qui do ${code_dir}/config/tabletools.do
	setlabels
	*formerly named table_ovb2
	dhoztab o_fd1 i_fd1 iw_fd1 o_fd2 i_fd2 iw_fd2 o_f i_f iw_f using ${tab_dir}/appendix/Table_A33_addcontrols.tex, b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines showdepvarinfo nomtitles sfmt(a1) scalars("has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label mgroups("Domestic and foreign" "Foreign", pattern(1 0 0 0 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(*zero* _cons spilloversown spilloversother stock*) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC msize_foreign msize Lintr_foreign Lintr spilloversown_foreignpw spilloversownpw spilloversother_foreignpw spilloversotherpw offshoring_foreign offshoring LSW_foreigniw LSWiw HSW_foreigniw HSWiw) ///
	order(LSW HSW GDPGAP VAEMP offshoring Lintr LSWiw HSWiw) ///
		depvar("Auto95") ///
		notes("This table tests three alternative explanations. Offshoring denotes the log weighted averages of the share of foreign value added in gross value added in manufacutring. Long-term interest rate denotes the real yield on 10-year government bonds. Low-skill wages (iw) and high-skill wages (iw) compute log weighted averages of wages in the countries where the firm's inventors are located. All columns include firm and industry-year fixed effects. Columns 4--9 add country-year fixed effects. Columns 7--9 use the normalized foreign variables as defined in the text. Low-skill wage (iw) and high-skill wage (iw) in Column 9 are still the total wages. Normalized offshoring is defined similarly to normalized foreign low-skill wages; normalized foreign long-term interest rate is defined like normalized foreign GDP gap. Standard errors are clustered at firm-level and reported in parentheses.")

	*again, brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}